﻿using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Web;
using System.Web.Hosting;

namespace WebHelper.Helpers
{
    /// <summary>
    /// works only with http context
    /// </summary>
    [Serializable]
    public class UrlHelper
    {

        public static string GetCompleteUri(string s)
        {
            string domain = HttpContext.Current.Request.Url.Scheme + "://" + (HttpContext.Current.Request.Url.Authority + (HttpContext.Current.Request.ApplicationPath == "/" ? "" : HttpContext.Current.Request.ApplicationPath));
            return new Uri(HttpContext.Current.Request.Url, s.Replace("~", domain)).ToString();
        }

        public static string GetPhysicalPath(string url)
        {
            string s = "";
            //try to return physical path for complete url
            try
            {
                string completeUrl = UrlHelper.GetCompleteUri(url);
                string domain = HttpContext.Current.Request.Url.Scheme + "://" + (HttpContext.Current.Request.Url.Authority + (HttpContext.Current.Request.ApplicationPath == "/" ? "" : HttpContext.Current.Request.ApplicationPath));
                s = HostingEnvironment.MapPath(completeUrl.ToLower().Replace(domain.ToLower(), "~"));
            }
            catch
            {
                //try to return physical path for relative path
                s = HostingEnvironment.MapPath(url);
            }
            return s;
        }

        public static bool IsInternalUri(string url)
        {
            string domain = HttpContext.Current.Request.Url.Scheme + "://" + (HttpContext.Current.Request.Url.Authority + (HttpContext.Current.Request.ApplicationPath == "/" ? "" : HttpContext.Current.Request.ApplicationPath));
            return UrlHelper.GetCompleteUri(url.ToLower()).Contains(domain.ToLower());
        }

        public static DateTime GetResourceModifiedDate(string url)
        {
            DateTime date = DateTime.Now;
            if (UrlHelper.IsInternalUri(url))
            {
                try
                {
                    string s = HostingEnvironment.MapPath(url/*.AbsolutePath*/);
                    FileInfo fi = new FileInfo(s);
                    date = fi.LastWriteTime;

                    //trace
                    Trace.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "check lastWritetime for internal file", typeof(UrlHelper).Name + "  ");

                }
                catch
                {
                }
            }
            else
            {
                WebRequest request = WebRequest.Create(url);
                request.Method = "HEAD";
                string ifModifiedSince = request.GetResponse().Headers[HttpResponseHeader.LastModified];
                DateTime.TryParse(ifModifiedSince, out date);

                //trace
                Trace.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "check ifmodifiedsince in respose header", typeof(UrlHelper).Name + "  ");
            }
            return date;
        }
    }
}

